import { api, http } from "../../http";
import { call, put, takeLatest } from "redux-saga/effects";

/***************************************************************** 发送验证码模块 **************************************************************/
//发送邮箱验证码
export const SET_LOGIN_SHOW_INFO = "login/setShowInfo";
export const SET_STATUS = "login/setStatus";
export const SET_LOGIN_STATUS = "login/set_login_status";
// 同步任务
const setShowInfo = (showInfo) => ({
    type: SET_LOGIN_SHOW_INFO,
    showInfo,
});
const setStatus = (status) => ({
    type: SET_STATUS,
    status,
});
// 异步任务
const requestSendCode = async (tel) => {
    const { code } = await http.post(api.LOGIN_SEND_CODE_API, { tel });
    return code;
};
// 完整的任务
function* fetchSendCode({ tel }) {
    yield put(setStatus(1));
    yield put(setShowInfo("正在发送中"));
    // 发送验证码

    const result = yield call(requestSendCode, tel);
    if (result !== 0) {
        yield put(setShowInfo("重新发送"));
        yield put(setStatus(3));
        return;
    }
    // 设置初始的倒计时
    let count = 60;
    yield put(setShowInfo(count + "s"));

    // 启动倒计时
    while (count > 0) {
        count = yield call(timer, count); // 设置数据5
        yield put(setShowInfo(count + "s"));
    }
    // 倒计时结束
    yield put(setShowInfo("重新发送"));
    yield put(setStatus(0));
}
// 倒计时
const timer = (count) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve(count - 1);
        }, 1000);
    });
};
function* sendCodeEffect() {
    yield takeLatest("login/send_code", fetchSendCode);
}

/*********************************************************** 核对验证码模块 ******************************************************************/
const requestCheckCode = async (tel, telCode ) => {
    const { code, data } = await http.post(api.LOGIN_CHECK_CODE_API, {
        tel,
        code: telCode,
    });
    return { code, data };
};
const setLoginStatus = (status) => ({
    type: SET_LOGIN_STATUS,
    status,
});
function* fetchCheckCode({ tel, code }) {
    yield put(setLoginStatus(1));
    const result = yield call(requestCheckCode, tel, code);
    if (result.code === 0) {
        // 登录成功，保存token
        localStorage.setItem("TOKEN", result.data);
        yield put(setLoginStatus(2));
    } else {
        // 登陆失败
        yield put(setLoginStatus(3));
    }
}

function* checkCodeEffect() {
    yield takeLatest("login/check_code", fetchCheckCode);
}

/*********************************************************** 检查是否过期模块 ******************************************************************/
const requestCheckLogin = async () => {
    const result = await http.get(api.LOGIN_CHECK_LOGIN_API);
    return result.code;
};
function* fetchCheckLogin() {
    const code = yield call(requestCheckLogin);
    if (code === 0) {
        // 未过期啥也不干
    } else {
        yield put(setLoginStatus(0));
        localStorage.removeItem("TOKEN");
    }
}

function* checkLoginEffect() {
    yield takeLatest("login/check_login", fetchCheckLogin);
}
export default [sendCodeEffect, checkCodeEffect, checkLoginEffect];
